/*
 * @Author: TC 
 * @Date: 2021-09-27 16:05:43 
 * @Last Modified by: TC
 * @Last Modified time: 2023-03-26 02:08:46
 */

import { mFrameConfigConst } from "../Const/FrameConfigConst";
import { mFrameStorageConst } from "../Const/FrameStorageConst";

/**
 * 本地数据类
 */
export class StorageManager  {

    /**
     * 初始化
     */
    public init(): void {
        this.setDefaultData();
    }

    /**
     * 设置默认数据
     */
    private setDefaultData(): void {

        let version = localStorage.getItem(mFrameStorageConst.VERSION_NUM);
        if (version != mFrameConfigConst.version) {
            localStorage.setItem(mFrameStorageConst.VERSION_NUM, mFrameConfigConst.version);
            this.setNumber(mFrameStorageConst.MUSIC_VAL, 1);
            this.setNumber(mFrameStorageConst.EFFECT_VAL, 1);
            this.setNumber(mFrameStorageConst.HIGHEST_STORE, 0);
            this.setNumber(mFrameStorageConst.CURR_MISSION, 0);
            this.setNumber(mFrameStorageConst.ENERY, mFrameConfigConst.enery);
            this.setNumber(mFrameStorageConst.MONEY, mFrameConfigConst.money);
        }
    }

    /**
     * 设置数字数据
     * @param key 
     * @param num 
     */
    public setNumber(key: string, num: number): void {
        localStorage.setItem(key, `${num}`);
    }

    /**
     * 获取数字数据
     * @param key 
     * @returns 
     */
    public getNumber(key: string): number {
        let num = Number(localStorage.getItem(key));
        return !isNaN(num) ? num : 0;
    }

    /**
     * 增加数字数据
     * @param key 
     * @param delta 
     */
    public addNumber(key: string, delta: number): void {
        let num = this.getNumber(localStorage.getItem(key));
        this.setNumber(key, num + delta);
    }

    /**
     * 设置字符串数据
     * @param key 
     * @param str 
     */
    public setString(key: string, str: string): void {
        localStorage.setItem(key, `${str}`);
    }

    /**
     * 获取字符串数据
     * @param key 
     * @returns 
     */
    public getString(key: string): string {
        return localStorage.getItem(key);
    }

    /**
     * 设置布尔数据
     * @param key 
     * @param b 
     */
    public setBoolean(key: string, b: boolean): void {
        localStorage.setItem(key, `${b ? 1 : 0}`);
    }

    /**
     * 获取布尔数据
     * @param key 
     * @returns 
     */
    public getBoolean(key: string): boolean {
        let num = Number(localStorage.getItem(key));
        return !isNaN(num) && 0 != num;
    }
}
